﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RonYee.Models;
using RonYee.Service;

namespace RonYee.Controllers
{
    public class CheckoutController : Controller
    {
        RongeDb db = new RongeDb();

        //const string PromoCode = "FREE";
        //
        // GET: /Checkout/

        [Authorize]
        public ActionResult CheckPhoneNumber() 
        {
            var user = from c in db.Customers
                       where c.Login == HttpContext.User.Identity.Name
                       select c;
            ViewData["user"] = user;
            var cart = ShoppingCart.GetCart(this.HttpContext);
            ViewBag.Total = cart.GetTotal();
            ViewBag.Error = "确认信息后，请提交订单！";
            return View();
        }

        [HttpPost]
        [Authorize]
        public ActionResult CheckPhoneNumber(FormCollection values) 
        {
            var user = (from u in db.Customers
                        where u.Login == User.Identity.Name
                        select u).FirstOrDefault();
            string No = Request.Form["UserNo"];
            string phone = Request.Form["Phone"];
            if (user.Phone != phone && user.CustomerNo != No) 
            {
                try
                {
                    user.CustomerNo = No;
                    user.Phone = phone;
                    UpdateModel(user);
                    db.SaveChanges();
                }
                catch
                {
                    ViewBag.Error = "订单提交失败，请重试！";
                    return View();
                }
            }
            else if (user.Phone == phone && user.CustomerNo != No)
            {
                try
                {
                    user.CustomerNo = No;
                    UpdateModel(user);
                    db.SaveChanges();
                }
                catch
                {
                    ViewBag.Error = "订单提交失败，请重试！";
                    return View();
                }
            }
            else if (user.Phone != phone && user.CustomerNo == No)
            {
                try
                {
                    user.Phone = phone;
                    UpdateModel(user);
                    db.SaveChanges();
                }
                catch
                {
                    ViewBag.Error = "订单提交失败，请重试！";
                    return View();
                }
            }
            var order = new Order();
            TryUpdateModel(order);
            try
            {
                //Process the order
                var cart = ShoppingCart.GetCart(this.HttpContext);
                decimal total = cart.GetTotal();
                order.Username = User.Identity.Name;
                order.OrderDate = DateTime.Now;
                order.UserNo = No;
                order.Phone = phone;
                order.Status = 0;
                order.Total = Convert.ToDecimal(total);

                //Save Order
                db.Orders.Add(order);
                db.SaveChanges();

                cart.CreateOrder(order);
                
                return RedirectToAction("Complete", new { id = order.OrderId });

            }
            catch
            {
                //Invalid - redisplay with errors
                ViewBag["Error"] = "订单提交失败，请重试！";
                return View(order);
                
            }

        }

        //
        // GET: /Checkout/Complete

        [Authorize]
        public ActionResult Complete(int id)
        {
            // Validate customer owns this order
            bool isValid = db.Orders.Any(
                o => o.OrderId == id &&
                o.Username == User.Identity.Name);

            if (isValid)
            {
                return View(id);
            }
            else
            {
                return View("Error");
            }
        }

    }
}
